package com.projectName.www.service;

import com.projectName.www.dao.*;
import com.projectName.www.po.Dish;
import com.projectName.www.po.Order;
import com.projectName.www.po.User;

import java.sql.Timestamp;
import java.util.List;

public class CtmServiceImpl extends UserServiceImpl implements CtmService{



    //查看订单状态（通过用户查找需要的订单）
    @Override
    public List<Order> showOrder(String uid){
        orderDaoImplement=new OrderDaoImpl();
        List<Order> orderByCtm = orderDaoImplement.findOrderByCtm(uid);
        return orderByCtm;
    }


    @Override
    public void makeOrder(String ctmid, String did) {
        //获取dish中的商家id,单价和数目
        UserDao userDao=new UserDaoImpl();
        OrderDao orderDao=new OrderDaoImpl();
        DishDao dishDao=new DishDaoImpl();

        Dish dish=dishDao.findByID(did);
        System.out.println(dish);//调试********************************8
        User ctm = userDao.findByID(ctmid);
        User slr = userDao.findByID(dish.getSlrid().toString());

        //        orderDao.insert(new Order(Timestamp oTime, Integer oNumber, BigDecimal tolPrice, String status, Integer ctmid, Integer strid));
        ctm.setMoney(ctm.getMoney().subtract(dish.getdPrice()));//余额减去价格
        slr.setMoney(slr.getMoney().add(dish.getdPrice()));
        int oid = orderDao.insert(new Order(new Timestamp(System.currentTimeMillis()), 1, dish.getdPrice(), "已确认",Integer.parseInt(ctmid), slr.getuId()));
        userDao.update(ctm);
        userDao.update(slr);

        //这个订单刚刚打印出来，如何获取他的id捏？
        //调用orderdao里面getgeneratedkeys方法来返回主键捏
        orderDao.connect(oid,Integer.parseInt(did));


    }
}
